import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import codeg.Codeg;
import gui.SWTCodegGUI;


public class FileCodegGUI implements SWTCodegGUI {
    private FileCodeg codeg;
    private Label label = null;
    private Button cbRead, cbWrite, cbBinary;
    
    public void populate(final Composite content)
    {
        content.setLayout(new GridLayout(1, false));
        
        label = new Label(content, SWT.LEFT);
        if (codeg.getPath() != null)
            label.setText("File: " + codeg.getPath().getAbsolutePath());
        else
            label.setText("File: No file selected");
        label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
        
        final Button buttonSelect = new Button(content, SWT.PUSH);
        buttonSelect.setText("Select file");
        buttonSelect.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));
        MouseListener selectML = new MouseListener()
        {
            public void mouseUp(MouseEvent e)
            {
                    edit(content);
            }
            public void mouseDown(MouseEvent e) {};
            public void mouseDoubleClick(MouseEvent e) {};
        };
        buttonSelect.addMouseListener(selectML);
        
        Label options = new Label(content, SWT.NONE);
        options.setText("Options");
        Font sf = content.getDisplay().getSystemFont();
        FontData[] fd = sf.getFontData();
        for (int i = 0; i < fd.length; i++)
            fd[i].setStyle(SWT.BOLD);
        Font bf = new Font(content.getDisplay(), fd);
        options.setFont(bf);
        options.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        
        Group g = new Group(content, SWT.NONE);
        g.setLayout(new FillLayout());
        
        cbRead = new Button(g, SWT.RADIO);
        cbRead.setText("Read");
        cbRead.setSelection(codeg.isRead());

        cbWrite = new Button(g, SWT.RADIO);
        cbWrite.setText("Write");
        cbWrite.setSelection(codeg.isWrite());
        
        cbBinary = new Button(content, SWT.CHECK);
        cbBinary.setText("Binary");
        cbBinary.setSelection(codeg.isBinary());
        cbBinary.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false));
        
        MouseListener optionsML = new MouseListener()
        {
            public void mouseUp(MouseEvent e)
            {
                    optionsChanged();
            }
            public void mouseDown(MouseEvent e) {};
            public void mouseDoubleClick(MouseEvent e) {};
        };
        cbRead.addMouseListener(optionsML);
        cbWrite.addMouseListener(optionsML);
        cbBinary.addMouseListener(optionsML);
    }
    
    private void optionsChanged() {
        codeg.setRead(cbRead.getSelection());
        codeg.setWrite(cbWrite.getSelection());
        codeg.setBinary(cbBinary.getSelection());
        codeg.changed();
    }
    
    private void edit(Composite content)
    {
        FileDialog dialog = new FileDialog(content.getShell(), SWT.OPEN);
        dialog.setText("Select a file");
        String fname = dialog.open();
        if (fname != null)
        {
            setPath(new File(fname));
            codeg.changed();
        }
    }
    
    public void setPath(File filePath) {
          codeg.setPath(filePath);
          if (label != null && codeg.getPath() != null)
                label.setText("File: " + codeg.getPath().getAbsolutePath());
    }

    public Codeg getCodeg() {
        return codeg;
    }

    public void setCodeg(Codeg codeg) {
        this.codeg = (FileCodeg) codeg;
    }

}
